//
//  HMStatusOriginalView.swift
//  Weibo
//
//  Created by 米易雅克 on 16/4/19.
//  Copyright © 2016年 米易雅克. All rights reserved.
//

import UIKit
//原创view
class HMStatusOriginalView: UIView {
    
    var status: HMStatus? {
    
        didSet {
        
            if let profile_image_url = status?.user?.profile_image_url {
                //头像有地址
                let url = NSURL(string: profile_image_url)!
               // iconView.sd_setImageWithURL(url, placeholderImage: UIImage.init(named: "avatar"))
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage.init(named: "avatar"), completed: { (image, error, _, _) in
                    if error != nil {
                        
                        HMPrint("设置头像出错了:\(error)")
                        return
                    }
                    
                    //将图片异步绘制成圆角
                    image.HM_AsyncDrawImage(nil,  isCorner: true, drawFinsh: { (image) in
                        self.iconView.image = image
                    })
      
                })
            }
            
        nameLabel.text = status?.user?.screen_name
        timeLabel.text = NSDate.date(status?.created_at ?? "刚刚").sinaDateDescription()
        sourceLabel.text = status?.source
        contentLabel.text = status?.text
        //设置会员等级
//            if let mbrank = status?.user?.mbrank {
//                if mbrank > 0 && mbrank <= 6 {
//                    //设置会员等级
//                    memberView.image = UIImage.init(named: "common_icon_membership_level\(mbrank)")
//                }else {
//                
//                //防止cell重用
//                    memberView.image = nil
//                }
//            }

        memberView.image = status?.user?.mbrankImage
            
        //认证类型
        verifiedView.image = status?.user?.verifiedImage
            
        //将微博模型的图片url设置给HMStatusPictureView,HMStatusPictureView会自己去计算宽高
        pictureView.pictureURLs = status?.storePictureURLs
            //HMPrint("\(status?.storePictureURLs)")
           // HMPrint("\(pictureView.pictureURLs)")
        // 当没有图片的时候,HMStatusPictureView距离contentLabel的底部不需要8的间距
//        pictureView.translatesAutoresizingMaskIntoConstraints = false
        pictureView.snp_updateConstraints { (make) in
            let margin = status?.storePictureURLs == nil ? 0 : HMStatusCellMargin
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)

            }
        }
    }
    override init(frame: CGRect) {
        
    super.init(frame: frame)
    backgroundColor = UIColor.whiteColor()
    prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI() {
    //添加子控件
    addSubview(iconView)
    addSubview(nameLabel)
    addSubview(timeLabel)
    addSubview(sourceLabel)
    addSubview(memberView)
    addSubview(verifiedView)
    addSubview(contentLabel)
    addSubview(pictureView)
        
    //添加子约束
    //头像
    iconView.snp_makeConstraints { (make) in
        make.top.leading.equalTo(self).offset(HMStatusCellMargin)
        make.size.equalTo(CGSize(width: HMStatusIconViewWH, height: HMStatusIconViewWH))
        
        }
    //名字
    nameLabel.snp_makeConstraints { (make) in
        make.top.equalTo(iconView)
        make.leading.equalTo(iconView.snp_trailing).offset(HMStatusCellMargin)
        }
    //时间
    timeLabel.snp_makeConstraints { (make) in
        make.bottom.equalTo(iconView)
        make.leading.equalTo(nameLabel)
        }
    //来源
    sourceLabel.snp_makeConstraints { (make) in
        make.bottom.equalTo(timeLabel)
        make.leading.equalTo(timeLabel.snp_trailing).offset(HMStatusCellMargin)
        }
    //会员等级
    memberView.snp_makeConstraints { (make) in
        make.centerY.equalTo(nameLabel)
        make.leading.equalTo(nameLabel.snp_trailing).offset(HMStatusCellMargin)
        make.size.equalTo(CGSize(width: 14, height: 14))
        }
    //认证图标
    verifiedView.snp_makeConstraints { (make) in
        make.centerX.equalTo(iconView.snp_trailing)
        make.centerY.equalTo(iconView.snp_bottom)
        make.size.equalTo(CGSize(width: 17, height: 17))
        }
    //微博内容
    contentLabel.snp_makeConstraints { (make) in
        make.leading.equalTo(self).offset(HMStatusCellMargin)
        make.top.equalTo(iconView.snp_bottom).offset(HMStatusCellMargin)
        make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        }
        
    //原创微博图片视图
    pictureView.snp_makeConstraints { (make) in
        make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
        make.leading.equalTo(self).offset(HMStatusCellMargin)
//        make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        //测试
//        make.height.equalTo(Int(arc4random_uniform(200) + 100))
        make.size.equalTo(CGSizeZero)
        }
    //自定义的原创微博view的底部和底部控件底部的底部重合,偏移8
    self.snp_makeConstraints { (make) in
        
        make.bottom.equalTo(pictureView).offset(HMStatusCellMargin)
        
        }
    }
    
//MARK: 懒加载子控件
    //头像
    private lazy var iconView: UIImageView = {
    let imageView = UIImageView.init(image: UIImage.init(named: "avatar"))
    
        //设置
    imageView.layer.cornerRadius = 17.5
    imageView.layer.masksToBounds = true
    return imageView
    }()
    //用户名称
    private lazy var nameLabel: UILabel = UILabel(fontSize: HMStatusNameFontSize, textColor: UIColor.darkGrayColor())
    

    
    //时间label
    private lazy var timeLabel: UILabel = UILabel(fontSize: HMStatusTimeFontSize, textColor: UIColor.orangeColor())
    
//    private lazy var timeLabel: UILabel = {
//    let label = UILabel()
//    
//    label.font = UIFont.systemFontOfSize(HMStatusTimeFontSize)
//    label.textColor = UIColor.orangeColor()
//        
//    return label
//    }()
    
    //来源label
    private lazy var sourceLabel: UILabel = UILabel(fontSize: HMStatusTimeFontSize, textColor: UIColor.lightGrayColor())
    

    
    //会员图标
//    private lazy var memberView: UIImageView = UIImageView(image: UIImage.init(named: "common_icon_membership_expired"))
    private lazy var memberView: UIImageView = UIImageView(imageNamed: "common_icon_membership_expired")
    
    //认证图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage.init(named: "avatar_vip"))
    
    //原创微博内容
    private lazy var contentLabel: UILabel = {
    let label = UILabel()
        
    label.font = UIFont.systemFontOfSize(HMStatusContentFontSize)
    label.textColor = UIColor.darkGrayColor()
    label.text = "大雨淋过，才知道拥有，不要知道最后才知道一无所有。一切要求都不是自己想要的需求大雨淋过，才知道拥有,知道"
    label.sizeToFit()
    label.numberOfLines = 0
    
    return label
    }()
    //原创微博配图
    private lazy var pictureView: HMStatusPictureView = HMStatusPictureView()
    
}
